package icu.which;

public class Main {
    //迭代矩阵
    public int[][] mat = new int[][]{
            {1,0,-1,0},
            {-1,0,0,1},
            {0,-1,1,-1},
            {-1,-1,-1,0}
    };
    //6996
    public int[] t = new int[]{6,9,9,6};
    //要计算的权重（方程组的解）
    public double[] w = new double[4];//权，初始为0
    //学习率
    public double n = 0.5;
    //误差指标
    public double E = 0.2;

    public void compute(){
        double[] a = new double[4];//存放输出的向量
        double e = 0;

        //计算输出误差
        for(int i=0;i<4;i++){
            e+=Math.pow(t[i]-a[i],2);
        }
        e = Math.sqrt(e);

        while (e>E)
        {
            //四次学习模式对
            for(int i=0;i<4;i++){//每个模式对
                for(int j=0;j<4;j++){//四个更新
                    this.w[j] += n*(t[i]-a[i])*mat[i][j];
                }

                //更新输出//用新权重计算输出
                for (int l=0;l<4;l++){
                    a[l] = mat[l][0]*w[0]+mat[l][1]*w[1]+mat[l][2]*w[2]+mat[l][3]*w[3];
                }
            }

            System.out.println("权重值"+this.w[0]+" "+this.w[1]+" "+this.w[2]+" "+this.w[3]);

            //计算输出误差
            e = 0;
            for(int i=0;i<4;i++){
                e+=Math.pow(a[i]-t[i],2);
            }
            e = Math.sqrt(e);

            System.out.println("e:"+e);
        }

        System.out.print("解:");
        for(int i=0;i<4;i++){
            System.out.print(a[i]+" ");
        }
    }

    public static void main(String[] args) {
        Main x = new Main();
        x.compute();
    }
}
